//
//  NewBecomeVipVc.m
//  Shengzhuli
//
//  Created by FXGuo on 2018/1/27.
//  Copyright © 2018年 kamangkeji. All rights reserved.
//

#import "NewBecomeVipVc.h"
#import <WebKit/WebKit.h>
#import <Masonry.h>
#import "NewPersonalNetWork.h"
#import "BecomeVipView.h"
#import "SZLAccountHelper.h"
#import "VipNetwork.h"
#import "SZLSinglePayViewController.h"
#import "MBProgressHUD+MJ.h"
#import "BackButton.h"

@interface NewBecomeVipVc ()
@property (weak, nonatomic) IBOutlet UILabel *nameLab;
@property (weak, nonatomic) IBOutlet WKWebView *webView;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *vipHeightCons;
@property (weak, nonatomic) IBOutlet UIView *headView;
@property (nonatomic,strong) NSMutableArray *members;

@end

@implementation NewBecomeVipVc

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setNavi];
    [self setView];
    self.nameLab.text = self.info.name;
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [self getMemberList];
    });
}
- (void)setNavi
{
    self.vipHeightCons.constant = 0.26*ScreenH;
    self.navigationController.navigationBar.translucent = YES;
    [self.navigationController.navigationBar setBackgroundImage:[[UIImage alloc]init] forBarMetrics:UIBarMetricsDefault];
    [self.navigationController.navigationBar setShadowImage:[[UIImage alloc]init]];
}
- (UIBarButtonItem *)customBackItemWithTarget:(id)target
                                       action:(SEL)action
{
    BackButton *backBtn = [[BackButton alloc]initWithFrame:CGRectMake(0, 0, 50, 20)];
    backBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    [backBtn setBackgroundImage:[UIImage imageNamed:@"titleBack"] forState:UIControlStateNormal];
    [backBtn addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
    backBtn.contentEdgeInsets = UIEdgeInsetsMake(0, -20, 0, 0);
    return [[UIBarButtonItem alloc]initWithCustomView:backBtn];
}
- (void)setView
{
    WKWebView *wk = [[WKWebView alloc]init];
    wk.scrollView.showsVerticalScrollIndicator = NO;
    wk.scrollView.showsHorizontalScrollIndicator = NO;
    [self.view addSubview:wk];
    [wk mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.headView.mas_bottom).offset(SafeAreaTopHeight-10);
        make.left.bottom.right.equalTo(self.view);
    }];
    [wk loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@/memberExample",PublicServer]]]];
}
- (IBAction)becomeClick:(UIButton *)btn
{
    UIWindow *keyW = [UIApplication sharedApplication].keyWindow;
    BecomeVipView *view = [[NSBundle mainBundle]loadNibNamed:@"BecomeVipView" owner:nil options:nil].lastObject;
    view.frame = CGRectMake(0, 0, ScreenW, ScreenH);
    view.backgroundColor = RGBA(0, 0, 0, 0.4);
    view.becomeBlock = ^(MemberModel *model) {
        [self gotoPay:model];
    };
    view.members = self.members;
    [keyW addSubview:view];
    [view animatedShow];
}
#pragma mark ----支付------
- (void)gotoPay:(MemberModel *)model
{
    NSMutableDictionary *param = [NSMutableDictionary dictionary];
    param[@"token"] = [SZLAccountHelper shareInstance].token;
    param[@"memberType"] = model.memberId;
    param[@"amount"] = @(model.memberMoney);
    [self showClearBgWaiter];
    [VipNetwork adderGotoPayWithParame:param response:^(BOOL success, NSString *msg,SingleCreateModel *sendModel) {
        [self hideWaiter];
        if (success) {
            SZLSinglePayViewController *szlPayVc = [[SZLSinglePayViewController alloc] init];
            szlPayVc.model = sendModel;
            szlPayVc.payChargeType = PayChargeTypeAdd;
            [self.navigationController pushViewController:szlPayVc animated:YES];
        }else {
            [MBProgressHUD showError:msg];
        }
    }];
}
#pragma mark ----角色列表------
- (void)getMemberList
{
    [self showWaiter];
    [NewPersonalNetWork getMemberListWithParame:nil response:^(BOOL success, NSString *msg, NSArray *lists) {
        [self hideWaiter];
        if (success) {
            [self.members addObjectsFromArray:lists];
        }else {
            
        }
    }];
}
- (NSMutableArray *)members
{
    if (!_members) {
        _members = [NSMutableArray array];
    }
    return _members;
}


@end
